package com.mmy.controller.car;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.car.TCarOrder;
import com.mmy.domain.car.TCarPlan;
import com.mmy.domain.car.VCarBase;
import com.mmy.domain.car.VCarPlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.car.impl.CarPlanServiceImpl;
import com.mmy.service.car.impl.TCarBaseServiceImpl;
import com.mmy.service.car.impl.TCarOrderServiceImpl;
import com.mmy.service.car.impl.TCarOrgServiceImpl;
import com.mmy.service.car.impl.TCarPlanServiceImpl;
import com.mmy.service.car.impl.TCarTypeServiceImpl;

@Controller
public class CarFrontController {
	
	@Autowired
	TCarBaseServiceImpl tCarBaseServiceImpl;
	@Autowired
	TCarTypeServiceImpl tCarTypeServiceImpl;
	@Autowired
	TCarOrgServiceImpl tCarOrgServiceImpl;
	@Autowired
	TCarOrderServiceImpl tCarOrderServiceImpl;
	@Autowired
	TCarPlanServiceImpl tCarPlanServiceImpl;
	@Autowired
	CarPlanServiceImpl carPlanServiceImpl;
	@Autowired
	Common common;
	
	/**
	 * 函数功能说明： 跳转--前台租车首页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期2021.03.02
	 */
	@RequestMapping(value = "/carindexfront")
	public String ToCarBase(Model model) {
		
		List<VCarBase> lstCarBase = tCarBaseServiceImpl.findAll();
		model.addAttribute("carBase", lstCarBase);
		return "car/carindex";
	}
	
	/**
	 * 函数功能说明： 跳转--去预留页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期2021.03.03
	 */
	@RequestMapping("/reservecar")
	public String EnterReserveCarBase(HttpServletRequest request, Model model) {

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		List<VCarPlan> lstCarPlan = tCarPlanServiceImpl.findByCarID(iCarID);
		model.addAttribute("plan", lstCarPlan);
		model.addAttribute("iCarID", iCarID);
		return "car/reservecar";
	}
	
	/**
	 * 函数功能说明： 提交--预留页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 liys
	 * @throws ParseException 
	 * @date 创建日期2021.03.03
	 */
	@RequestMapping(value = "/subreservecarbase")
	public String ReserveCarBase(TCarOrder tCarOrder, HttpSession session,HttpServletRequest request) throws ParseException{
		
		Integer iCarID = Integer.parseInt(request.getParameter("CarID"));
		String strDate = request.getParameter("date");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(strDate);
		int iPlanID =  carPlanServiceImpl.findPlanById(iCarID, date);
		// 获取最大ID
		tCarOrder.setID(common.createMaxID(UtilConstants.TableName.T_CAR_ORDER));
		// 获得订单编号
		String cOrderID = common.createOrderCode(UtilConstants.TableName.T_CAR_ORDER, iPlanID);
		TCarPlan tCarPlan = tCarPlanServiceImpl.findByPlanID(iPlanID);
		tCarOrder.setCreateDate(new Date());
		tCarOrder.setReserveDate(new Date());
		tCarOrder.setFloor(tCarPlan.getFloor());
		tCarOrder.setPeer(tCarPlan.getPeer());
		tCarOrder.setPrice(tCarPlan.getPrice());
		BigDecimal bPrice = tCarPlan.getPrice();
		tCarOrder.setOrderAmount(bPrice);
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		tCarOrder.setCreator(tSysUser.getUserName());
		tCarOrder.setReserveUserID(tSysUser.getUserID());
		tCarOrder.setOrderID(cOrderID);
		tCarOrderServiceImpl.save(tCarOrder.getID());
		tCarOrder.setOrderState((byte) 0);
		tCarOrder.setCashierState((byte) 0);
		tCarOrder.setAccountState((byte) 0);
		tCarOrder.setInvoiceState((byte) 0);
		tCarOrder.setRefundState((byte) 0);
		tCarOrder.setRemittanceState((byte) 0);
		tCarOrderServiceImpl.save(tCarOrder);
		return "redirect:carindexfront";
	}
	
	/**
	 * 函数功能说明： 跳转--详情页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期2021.03.04
	 */
	@RequestMapping(value = "/carinfofront")
	public String ToCarBaseInfo(HttpServletRequest request, Model model) {

		int iCarID = Integer.parseInt(request.getParameter("iCarID"));
		VCarBase vCarBase = tCarBaseServiceImpl.findByID(iCarID);
		model.addAttribute("vCarBase", vCarBase);
		return "car/carinfo";
	}
}
